package com.abbas.smartCity.ui;

import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.text.Html;
import android.view.View;
import android.widget.ListView;

import com.abbas.smartCity.App;
import com.abbas.smartCity.R;
import com.abbas.smartCity.adapter.ListCommAdapter;
import com.abbas.smartCity.base.BaseActivity;
import com.abbas.smartCity.bean.HomeNewsBean;
import com.abbas.smartCity.retroft.ICallback;
import com.bumptech.glide.Glide;

import java.util.List;

import retrofit2.Call;

/**
 * 搜索结果页面
 */
public class SearchResultActivity extends BaseActivity {

    private ListView lv;
    private ListCommAdapter<HomeNewsBean.RowsBean, ListCommAdapter.NewsHolder> adapter;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_search_result;
    }

    @Override
    protected String setCustomTitle() {
        return "搜索结果";
    }

    @Override
    protected void initView() {
        lv = (ListView) findViewById(R.id.lv);
    }

    @Override
    protected void initData() {
        //获取传递过来的新闻ID
        final String data = getIntent().getStringExtra("data");
        //获取新闻
        api.getHomeNews(null,data).enqueue(new ICallback<HomeNewsBean>() {
            @Override
            public void onSuccess(Call<HomeNewsBean> call, HomeNewsBean data) {
                final List<HomeNewsBean.RowsBean> rows = data.getRows();
                if (adapter == null){
                    adapter = new ListCommAdapter<HomeNewsBean.RowsBean, ListCommAdapter.NewsHolder>(
                            rows,
                            ListCommAdapter.NewsHolder.class,
                            R.layout.news_item
                    ) {
                        @SuppressLint("SetTextI18n")
                        @Override
                        protected void show(HomeNewsBean.RowsBean data, View convertView, NewsHolder viewHolder, int position, Context context) {
                            viewHolder.title.setText(data.getTitle());
                            Glide.with(context)
                                    .load(App.getBaseUrl() + data.getCover())
                                    .into(viewHolder.im);
                            viewHolder.sub.setText(Html.fromHtml(data.getContent()));
                            viewHolder.tag1.setText("评论总数：" + data.getCommentNum());
                            viewHolder.tag2.setText("发布时间：" + data.getPublishDate());
                        }
                    };
                    lv.setAdapter(adapter);
                }else {
                    adapter.resetData(rows);
                }
            }
        });

    }

    @Override
    protected void initEvent() {
        lv.setOnItemClickListener((adapterView, view, i, l) -> {
            final Intent intent = new Intent(SearchResultActivity.this, DetailActivity.class);
            final HomeNewsBean.RowsBean item = adapter.getItem(i);
            intent.putExtra("id",item.getId());
            startActivity(intent);
        });
    }
}